package com.bailian.service;

import com.alibaba.fastjson.JSONObject;
import com.bailian.constant.Constants;
import com.bailian.core.utils.RedisUtil;
import com.bailian.weixin.api.IVerificaCodeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class VerificaCodeService extends BaseApiService<JSONObject> implements IVerificaCodeService {
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public BaseResponse<JSONObject> verifyWeixinCode(String phone, String weixinCode) {
        if (StringUtils.isEmpty(phone)) {
            return setResultError("手机号码不能为空!");
        }
        if (StringUtils.isEmpty(weixinCode)) {
            return setResultError("注册码不能为空!");
        }
        String code = redisUtil.getString(Constants.WEIXINCODE_KEY + phone);
        if (StringUtils.isEmpty(code)) {
            return setResultError("注册码已经过期,请重新发送验证码");
        }
        if (!code.equals(weixinCode)) {
            return setResultError("注册码不正确");
        }
        return setResultSuccess("注册码正确");
    }
}
